package cn.edu.xmu.tangzk.scala.s99

import scala.collection.immutable.Nil

object P01 {

    def lastBuiltIn[A](ls: List[A]): A = ls.last

    def lastRecursive[A](ls: List[A]): A = {
        ls match {
            case h :: Nil => h
            case _ :: tail => lastRecursive(tail)
            case _ => throw new NoSuchElementException
        }
    }

    def main(args: Array[String]) {
        println(lastBuiltIn(List(1, 1, 2, 3, 5, 8)))
        println(lastRecursive(List(1, 1, 2, 3, 5, 8)))
    }
}